package org.cevinedit.editor.view.dialogs;

import org.cevinedit.core.tools.CevineditEcoreTools;
import org.cevinedit.editor.view.toolgenerator.CevineditToolGenerator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cevinedit.presentation.CevineditEditor;

public class PolygonPointsDialog extends Dialog implements SelectionListener {

	protected CevineditEditor _editor = null;
	protected EClass _selectedEClass = null;
	protected String _returnPoints = "";

	protected List _listPoints = null;
	protected Text _pointX = null;
	protected Text _pointY = null;	
	protected String _pX = "";
	protected String _pY = "";
	public String getPoints()
	{
		return _returnPoints;
	}
	
	public PolygonPointsDialog(CevineditEditor editor, Shell parentShell, EClass selectedEClass) 
	{
		super(parentShell);
		_editor = editor;
		_selectedEClass = selectedEClass;
	}
	
	protected Control createDialogArea(Composite parent) 
	{
		Composite composite = ( Composite )super.createDialogArea(parent);
		composite.setLayout(new FillLayout());
		
		try 
		{
			composite.getShell().setText(_editor.getTextLang("PolygonPointsDialog", "DialogTitle"));

			Composite compostie_children = new Composite(composite, SWT.NONE);
			
			RowLayout layout_children = new RowLayout(SWT.VERTICAL);
			layout_children.marginHeight = 10;
			layout_children.marginWidth = 10;
			layout_children.wrap = true;
			compostie_children.setLayout(layout_children);
			compostie_children.setSize(composite.getSize().x, 500);
			
			Composite label_text_points_composite = new Composite(compostie_children, SWT.NONE);
			RowLayout layout_label_text_points = new RowLayout(SWT.HORIZONTAL);
			layout_label_text_points.marginHeight = 5;
			layout_label_text_points.marginWidth = 10;
			
			label_text_points_composite.setLayout(layout_label_text_points);			
			
			Label helpTextPoints = new Label(label_text_points_composite, SWT.NONE);
			helpTextPoints.setText("Introduce in the next fields the coordinate X and Y that \nconforms a point (x,y) on the polygon to create.");
						
			
			Composite add_point_composite = new Composite(compostie_children, SWT.NONE);
			add_point_composite.setSize(600, 500);
			RowLayout layout_add_point = new RowLayout(SWT.HORIZONTAL);
			layout_add_point.marginHeight = 5;
			layout_add_point.marginWidth = 15;
			add_point_composite.setLayout(layout_add_point);

			Label labelX = new Label(add_point_composite, SWT.NONE);			
			labelX.setText("X:");
			
			_pointX = new Text(add_point_composite, SWT.BORDER);
			_pointX.setSize(200, 30);
			
			
			Label labelY = new Label(add_point_composite, SWT.NONE);			
			labelY.setText("  Y:");			
			_pointY = new Text(add_point_composite, SWT.BORDER);
			_pointY.setSize(200, 30);
			
			Label labelSpace = new Label(add_point_composite, SWT.NONE);			
			labelSpace.setText("   ");		
			
			
			Button btnAddPoint = new Button(add_point_composite, SWT.PUSH);
			btnAddPoint.setText(_editor.getTextLang("PolygonPointsDialog", "AddBtn"));
			
			btnAddPoint.addSelectionListener(this);

			Composite label_list_points_composite = new Composite(compostie_children, SWT.NONE);
			RowLayout layout_label_list_point = new RowLayout(SWT.HORIZONTAL);
			layout_label_list_point.marginHeight = 5;
			layout_label_list_point.marginWidth = 10;
			
			label_list_points_composite.setLayout(layout_label_list_point);			
			
			Label helpListPoints = new Label(label_list_points_composite, SWT.NONE);
			helpListPoints.setText("The next list show the list of points (x,y) which conforms \nthe polygon figure. On the right appears an example of polygon.");
			
			Composite list_point_composite = new Composite(compostie_children, SWT.NONE);
			RowLayout layout_list_point = new RowLayout(SWT.HORIZONTAL);
			layout_list_point.marginHeight = 10;
			layout_list_point.marginWidth = 10;
			
			list_point_composite.setLayout(layout_list_point);
			list_point_composite.setSize(compostie_children.getSize().x, 500);
			_listPoints = new List(list_point_composite, SWT.BORDER | SWT.V_SCROLL);
			_listPoints.setSize(300, 450);		    

			
		    if (this._editor.getActualPersonalization().containsKey("pointsPolygon"))
		    {
		    	String points = this._editor.getActualPersonalization().get("pointsPolygon");
		    	if (!points.equals(""))
		    	{
		    		String points_list[] = points.split(";");
		    		
		    		for(String point : points_list)
		    		{
		    			_listPoints.add(point);
		    		}
		    	}
		    }
			Button btnDeletePoint = new Button(list_point_composite, SWT.PUSH);
			btnDeletePoint.setText(_editor.getTextLang("PolygonPointsDialog", "DeleteBtn"));
			btnDeletePoint.addSelectionListener(this);		    
			Label label = new Label(list_point_composite, SWT.NONE);
			label.setSize(100, 100);			
			label.setImage(new Image(this.getParentShell().getDisplay(), PolygonPointsDialog.class.getResourceAsStream("/images/points.jpg")));
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}

		return composite;
	}
	
	private void generatePoints()
	{
		_returnPoints = "";
		for (String point : _listPoints.getItems()) 
		{
			_returnPoints += point+";";
		}

	}
	@Override
	public void widgetSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() instanceof Button)
		{
			if (((Button)e.getSource()).getText().equals(_editor.getTextLang("PolygonPointsDialog", "AddBtn")))
			{
				_listPoints.add(_pointX.getText()+","+_pointY.getText());
				this.generatePoints();
			}
			if (((Button)e.getSource()).getText().equals(_editor.getTextLang("PolygonPointsDialog", "DeleteBtn")))
			{
				if (_listPoints.getSelectionIndex() >=0)
					_listPoints.remove(_listPoints.getSelectionIndex());
				this.generatePoints();
			}			
			
		}
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub

	}

}
